import time
from time import sleep
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class Base():
     def __init__(self):
         cap={
             "platformName": "Android",
             "deviceName": "emulator-5554",
             "appPackage": "com.kongfz.app",
             "appActivity": "com.kongfz.app.home.mvvm.homepage.HomeActivity",
             "noReset": True
         }
         self.driver = webdriver.Remote('	http://127.0.0.1:4723/wd/hub',cap)
         self.driver.implicitly_wait(18)
         sleep(15)


     def loc_ele(self,loc):
         try:
             return self.driver.find_element(*loc)
         except Exception as e:
             print("未找到定位元素的定位置",loc)

     def loc_wait_ele(self,loc):
         try:
             return WebDriverWait(self,15).until(EC.presence_of_element_located(loc))
         except Exception as e:
             print("未找到定位元素的定位置",loc)

     def loc_ui_ele(self,loc):
         try:
             return self.driver.find_element_by_android_uiautomator(loc)
         except Exception as e:
             print("未找到定位元素的定位置",loc)

     def jieping(self,filename):
         t=time.strftime('%Y%m%d%H%M%S',time.localtime())
         self.driver.save_screenshot(t+filename)

     def get_key_value(self,loc,key):
         if type(loc)==str:
             ele=self.loc_ui_ele(loc)
         else:
             ele=self.loc_ele(loc)

         return ele.get_attribute(key)

     def quit_driver(self):
         self.driver.quit()